#ifndef VECTOR_GRAPHIC_H
#define VECTOR_GRAPHIC_H

#include "meshdef.h"
#include <list>
#include "shader.h"
#include "BSplineCurve.h"
#include <GL/glut.h>
#include <vector>
#include <string>
#include "definitions.h"
#include "vector_math.h"

using namespace std;

// Defines an axis-aligned bounding box
struct Bounds
{
    vec3d p1;
    vec3d p2;
};

// Defines a vertex on the curve
struct Vertex 
{
    bool insideMask[3];
    vec3d onCurvePoint;
    vec3d prevOffCurvePoint;
    vec3d nextOffCurvePoint;

    Vertex()
    {
        insideMask[0] = false;
        insideMask[1] = false;
        insideMask[2] = false;
    };
};

class Graphic
{
public:
    bool            showControls;
    list<Vertex>    points; 
    Bounds          bounds;
    vec3d           color;

    Graphic();
    ~Graphic();

    virtual void draw() = 0;
    virtual void drawControlGeometry() = 0;
    Vertex* addPoint(vec3d point);
    void    close();
    void    controlUpdated(Vertex*);
protected:
    Shader* shader;
    BSplineCurve* curve;
    PolyMesh innerMesh;

    void generateInnerPolygon();
    void loadShader(std::string filename);
    bool isCurveClosed;
private:

    bool    isInsideCurve(const vec3d& testPoint, 
					      const vec<vec3d> polygon, 
                          int ignoredAxis);

    bool    isRayIntersected(vec3d lineOrigin, vec3d lineDirection, 
                             vec3d testRayOrigin, vec3d testRayDirection);


};

#endif